package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.Repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl  implements  DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
//        将paramap转换成对象
        Department department= JSONObject.parseObject(JSONObject.toJSONString(paramMap),Department.class);
//        进行保存数据
        Department department1= departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
//        判断是否存在
        if (department1 != null) {
            department1.setIsDeleted(0);
            department1.setUpdateTime(new Date());
            departmentRepository.save(department1);
        }
        else {
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            department.setUpdateTime(new Date());
            System.out.println("保存数据成功");
            departmentRepository.save(department);
        }

    }
//    分页查询
    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable= PageRequest.of(page-1,limit,sort);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        //创建实例
        Example<Department> example = Example.of(department, matcher);
        Page<Department> pages =departmentRepository.findAll(example,pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> list=new ArrayList<>();
//        根据医院编号查询医院所有的科室数据
        Department department=new Department();
        department.setHoscode(hoscode);
        Example example=Example.of(department);
        List<Department> all = departmentRepository.findAll(example);
//        根据大科室分类
        Map<String, List<Department>> collect = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry:collect.entrySet()){
//            大科室的编号
            String key = entry.getKey();
//            每个科室的数据
            List<Department> value = entry.getValue();
//            封装大科室
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(value.get(0).getBigname());
//            封装小科室
            List<DepartmentVo> dvoList=new ArrayList<>();
            for (Department department1:value
                 ) {
                DepartmentVo departmentVo1=new DepartmentVo();
                departmentVo1.setDepname(department1.getDepname());
                departmentVo1.setDepcode(department1.getDepcode());
                dvoList.add(departmentVo1);
            }
//              小科室放在大科室里面
            departmentVo.setChildren(dvoList);
            list.add(departmentVo);
        }
        return  list;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return  department.getDepname();
        }
        return "不存在";
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return  department;
    }


}
